package com.by.controller;

import cn.hutool.core.bean.BeanUtil;
import com.by.bean.User;
import com.by.common.bean.PageBean;
import com.by.common.bean.ResultBean;
import com.by.common.controller.BaseController;
import com.by.common.group.ISave;
import com.by.common.group.IUpdate;
import com.by.query.UserQuery;
import com.by.service.UserService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.DispatcherServlet;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
@Api(tags = "用户管理")
@RestController
@RequestMapping("/v1")
public class UserController extends BaseController {

    @Autowired
    UserService userService;


    @ApiImplicitParam(name = "account",paramType = "query",value = "用户名",dataTypeClass = String.class,required = true)
    @ApiOperation("账号唯一检查")
    @GetMapping("/user/check")
    public ResultBean checkUser(@RequestParam(name = "account",required = true) String account){
        int rs = userService.queryUserByAccount(account);
        String tmp = rs == 1 ? "no":"yes";
        return success("账号唯一检查",tmp);
    }

    @ApiOperation("用户列表")
    @PostMapping("/user/query")
    public ResultBean query(@RequestBody UserQuery query){
        Map params = BeanUtil.beanToMap(query);
        PageBean bean = userService.query(params);
        return success("用户列表",bean);
    }

    /*@Autowired
    HttpServletRequest request;

    @ApiOperation("保存用户")
    @PostMapping("/user")
    public ResultBean save(@Validated ({ISave.class}) @RequestBody User user, BindingResult result){
        validate(result);
        int rs = userService.save(user);
        return saveResult(rs);
    }

    @ApiOperation("修改用户")
    @PutMapping("/user")
    public ResultBean update(@Validated({IUpdate.class}) @RequestBody User user,BindingResult result){
        validate(result);
        int rs = userService.update(user);
        return updateResult(rs);
    }


    @ApiOperation("删除用户")
    @DeleteMapping("/user")
    public ResultBean delete(@ApiParam(name = "id", value = "要删除的用户id") @RequestParam(name = "id", required = true, defaultValue = "0") int id) {
        int rs = userService.delete(id);
        return deleteResult(rs);
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前第几页", dataTypeClass = Integer.class, required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页记录数", dataTypeClass = Integer.class, required = true, paramType = "query")
    })

    @ApiOperation("查询用户")
    @GetMapping("/user")
    public ResultBean<PageBean> query(@ApiIgnore @RequestParam Map map){
        DispatcherServlet dispatcherServlet;
        String path=request.getServletContext().getRealPath("/");
        System.out.println(path);
        PageBean bean = userService.query(map);
        return success("用户列表",bean);
    }*/
}









